package com.cloudkinto.config;

import com.cloudkinto.inteceptors.UserSessionProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.nio.charset.Charset;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private UserSessionProcessor sessionProcessor;


    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {//跨域全局配置
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {//拦截器配置 可添加多个拦截器
        registry.addInterceptor(sessionProcessor)
                .addPathPatterns("/api/**")//拦截的请求
                .excludePathPatterns("/api/applet/recharge/wxPayNotify")
                .excludePathPatterns("/api/user/login");//不拦截的请求
        super.addInterceptors(registry);
    }

}
